<template>
  <div class="login-container">
    <div class="box_div">
      <div class="left">
        <h2 class="box_h2">Welcome to</h2>
        <h2 class="box_h2">Tianhai Space Positioning System</h2>
        <p>数据采集 | 存储计算 | 工艺组态 </p>
        <p>工业应用一体化物联网平台</p>
      </div>
      <div class="right">
        <el-form
          ref="loginForm"
          :model="loginForm"
          :rules="loginRules"
          class="login-form"
          auto-complete="on"
          label-position="left"
        >
          <div class="title-container">
            <h2 class="title">手机验证码登录</h2>
            <p>Hello!欢迎登录后台管理系统</p>
          </div>

          <el-form-item prop="username" style="border: 1px solid #dcdfe6;">
            <span class="svg-container">
              <i class="el-icon-mobile-phone" style="font-size: 15px;" />
            </span>
            <el-input
              ref="username"
              v-model="loginForm.username"
              placeholder="手机号"
              name="username"
              type="text"
              tabindex="1"
              size="mini"
              auto-complete="on"
            />
          </el-form-item>

          <el-form-item prop="password" style="border: 1px solid #dcdfe6;">
            <span class="svg-container">
              <i class="el-icon-message" style="font-size: 15px;" />
            </span>
            <el-input
              :key="passwordType"
              ref="password"
              v-model="loginForm.password"
              size="mini"
              type="text"
              placeholder="验证码"
              name="password"
              tabindex="2"
              auto-complete="on"
              @keyup.enter.native="handleLogin"
            />
            <span v-if="!isCountDown" class="show-pwd" @click="showPwd">获取验证码</span>
            <span v-else class="show-pwd box_ball_second_color">{{second}}秒</span>
          </el-form-item>
          <p class="yzm"><span @click="$router.push('/login')">用户登录</span></p>

          <el-button
            :loading="loading"
            type="primary"
            style="width: 100%; margin-bottom: 30px"
            @click.native.prevent="handleLogin"
          >登录</el-button>

          <el-button
            plain
            style="width: 100%; margin-bottom: 30px; margin-left: 0"
            @click="$router.push('/register')"
          >注册</el-button>
        </el-form>
      </div>
    </div>
  </div>
</template>

<script>
import { validMobile } from '@/utils/validate'

export default {
  name: 'Register',
  data() {
    const validateUsername = (rule, value, callback) => {
      if (!validMobile(value)) {
        callback(new Error('请输入正确的手机号'))
      } else {
        callback()
      }
    }
    const validatePassword = (rule, value, callback) => {
      if (value.length < 6) {
        callback(new Error('请输入验证码'))
      } else {
        callback()
      }
    }
    return {
      second: 60,
      timer: null,
      isCountDown: false,
      loginForm: {
        username: '',
        password: ''
      },
      loginRules: {
        username: [
          {
            required: true,
            trigger: 'blur',
            validator: validateUsername
          }
        ],
        password: [
          {
            required: true,
            trigger: 'blur',
            validator: validatePassword
          }
        ]
      },
      loading: false,
      passwordType: 'password',
      redirect: undefined
    }
  },
  watch: {
    $route: {
      handler: function(route) {
        this.redirect = route.query && route.query.redirect
      },
      immediate: true
    }
  },
  methods: {
    showPwd() {
      if(validMobile(this.loginForm.username)) {
        this.isCountDown = true
        this.timer = setInterval(() => {
          if(this.second == 0) {
            this.isCountDown = false
            clearInterval(this.timer)
            this.second = 60
            this.timer = null
            return
          }
          this.second--
        }, 1000);
      } else {
        this.$message.warning('请输入正确的手机号')
      }
    },
    handleLogin() {
      this.$refs.loginForm.validate((valid) => {
        if (valid) {
          this.loading = true
          this.$store
            .dispatch('user/login', { username: "admin", password: "123456" })
            .then(() => {
              this.$router.push({ path: this.redirect || '/' })
              this.loading = false
            })
            .catch(() => {
              this.loading = false
            })
        } else {
          console.log('error submit!!')
          return false
        }
      })
    }
  }
}
</script>

<style lang="scss">
/* 修复input 背景不协调 和光标变色 */
/* Detail see https://github.com/PanJiaChen/vue-element-admin/pull/927 */

$bg: #fff;
$light_gray: #fff;
$cursor: #000;

@supports (-webkit-mask: none) and (not (cater-color: $cursor)) {
  .login-container .el-input input {
    color: $cursor;
  }
}

/* reset element-ui css */
.login-container {
  .el-form-item__content {
    height: 40px;
    line-height: 26px;
  }
  .el-input {
    display: inline-block;
    height: 47px;
    width: 85%;

    input {
      background: transparent;
      border: 0px;
      border-radius: 0px;
      padding: 12px 5px 12px 15px;
      color: #000;
      height: 40px;
      caret-color: #000;

      &:-webkit-autofill {
        box-shadow: 0 0 0px 1000px $bg inset !important;
        -webkit-text-fill-color: $cursor !important;
      }
    }
  }
}
</style>

<style lang="scss" scoped>
$bg: #2d3a4b;
$dark_gray: #889aa4;
$light_gray: #eee;

.login-container {
  min-height: 100%;
  width: 100%;
  // background-color: $bg;
  background: url(../../assets/img/logo_box.jpg);
  background-repeat: no-repeat;
  background-size: cover;
  overflow: hidden;

  .yzm {
    text-align: right;
    padding: 0px;
    cursor: pointer;
    margin-bottom: 20px;
    font-size: 14px;
    margin-top: -10px;
    color: rgba(128, 128, 128, 1);
    user-select: none;
  }

  & > div {
    // width: 1128px;
    // height: 695px;
    width: 57%;
    height: 70%;
    opacity: 1;
    border-radius: 8px;
    background: rgba(255, 255, 255, 1);
    // box-shadow: 0px 10px 20px 0px rgba(219, 231, 238, 1);
    position: fixed;
    left: 50%;
    // top: 50%;
    top: 53%;
    transform: translateX(-50%) translateY(-50%);
    box-sizing: border-box;
    display: flex;
    .left {
      width: 432px;
      height: 100%;
      background-color: rgba(24, 144, 255, 1);
      text-align: center;
      padding-top: 112px;
      box-sizing: border-box;
      color: #fff;
      & > h2 {
        margin-top: 0;
        font-size: 30px;
      }
      & > p {
        font-size: 16px;
      }
    }
  }

  .login-form {
    position: relative;
    width: 520px;
    max-width: 100%;
    padding: 160px 35px 0 132px;
    box-sizing: border-box;
    margin: 0 auto;
    overflow: hidden;
  }

  .svg-container {
    padding: 6px 5px 6px 15px;
    color: $dark_gray;
    vertical-align: middle;
    width: 30px;
    display: inline-block;
  }

  .title-container {
    position: relative;

    .title {
      font-size: 20px;
      color: rgba(0, 0, 0, 1);
      margin: 0px auto 40px auto;
      font-weight: bold;
      margin-bottom: 0;
    }
    & > p {
      margin-top: 8px;
      color: rgba(128, 128, 128, 1);
    }
  }

  .show-pwd {
    position: absolute;
    right: 10px;
    top: 7px;
    font-size: 14px;
    color: #409eff;
    cursor: pointer;
    user-select: none;
  }
  .box_ball_second_color {
    font-size: 14px;
    color: #889aa4;
  }
}
@media screen and (max-width: 1740px) {
  .box_h2 {
    font-size: 24px !important;
  }
}
@media screen and (max-height: 898px) {
  .login-container .login-form {
    padding: 98px 35px 0px 132px !important;
  }
}

@media screen and (max-width: 1521px) {
  .login-container > div {
    width: 935px !important;
    height: 559px !important;
  }
}
</style>
